package com.wangjiacai.hqlQuery.hql.condition.where;

import com.wangjiacai.hqlQuery.hql.condition.Condition;
import com.wangjiacai.hqlQuery.util.HqlUtil;

import java.lang.annotation.*;


@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Condition(condition = Like.LikeCondition.class,column = Like.LikeCondition.class,userValue = true,format = "%%%s%%")
public @interface Like {

    String column() default "";

    String hql() default "";

    class LikeCondition implements Condition.Where<Like,String>, Condition.Column<Like> {

        @Override
        public String where(Like like, String property, String column,Object obj, String value) {
            if(like==null) return column+" like '"+value+"'";
            return like.hql().equals("")?column+" like '"+value+"'":column+" like ("+ HqlUtil.parseHql(like.hql(),obj)+")";
        }

        @Override
        public String column(Like like, String property) {
            return like.column().equals("")?property:like.column();
        }
    }


}
